//
//  PGAllViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/10/3.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGAllViewController.h"
#import "PGHTTPSessionManager.h"
#import "PGPostViewCell.h"
#import "PGPostItem.h"
#import <MJExtension/MJExtension.h>
#import "PGPostViewModel.h"

#import <MJRefresh/MJRefresh.h>
@interface PGAllViewController ()

/**manager*/
@property (nonatomic, weak) PGHTTPSessionManager *manager;
/**posts*/
@property (nonatomic, strong) NSArray *posts;

/**postViewModels*/
@property (nonatomic, strong) NSMutableArray *postViewModels;

/**maxtime*/
@property (nonatomic, assign) NSUInteger maxtime;


@end

@implementation PGAllViewController

#pragma mark -懒加载manager
static NSString *const allID = @"postCell";
- (PGHTTPSessionManager *)manager {
    if (_manager == nil) {
        _manager = [PGHTTPSessionManager manager];
    }
    return _manager;
}

#pragma mark -懒加载视图模型数组
- (NSMutableArray *)postViewModels {
    if (_postViewModels == nil) {
        _postViewModels = [NSMutableArray array];
    }
    return _postViewModels;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = commonBgColor;
    self.tableView.contentInset = UIEdgeInsetsMake(navigationBarHeight+titleHeight, 0, tabBarHeight, 0);
    
    [self.tableView registerClass:[PGPostViewCell class] forCellReuseIdentifier:allID];
    
    [self loadNewData];
    
    [self setupRefreshView];
}

- (void)setupRefreshView {
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
//    headerView.backgroundColor = [UIColor redColor];
//    self.tableView.tableHeaderView = headerView;;
    
//    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
//    NSLog(@"%@",NSStringFromUIEdgeInsets(self.tableView.contentInset));
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    header.automaticallyChangeAlpha = YES;
    self.tableView.mj_header = header;
    
    [self.tableView.mj_header beginRefreshing];
    
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer = footer;
}

- (void)loadMoreData {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"data";
    parameters[@"type"] = @(PGPostTypeText);  //1为全部，10为图片，29为段子，31为音频，41为视频，默认为1
    parameters[@"maxtime"] = @(_maxtime);
    
    __weak typeof(self) weakSelf = self;
    [self.manager GET:PGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        //        [responseObject writeToFile:@"/Users/pengpeng/Desktop/text.plist" atomically:YES];
        [self.tableView.mj_header endRefreshing];
        _maxtime = [responseObject[@"info"][@"maxtime"] integerValue];
        NSArray *posts = [PGPostItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        for (PGPostItem *item in posts) {
            PGPostViewModel *postViewModel = [[PGPostViewModel alloc] init];
            
            postViewModel.item = item;
            [weakSelf.postViewModels addObject:postViewModel];
            
        }
        
        
        [weakSelf.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}



- (void)loadNewData {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"data";
    parameters[@"type"] = @(PGPostTypeText);  //1为全部，10为图片，29为段子，31为音频，41为视频，默认为1
    __weak typeof(self) weakSelf = self;
    [self.manager GET:PGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
//        [responseObject writeToFile:@"/Users/pengpeng/Desktop/text.plist" atomically:YES];
        _maxtime = [responseObject[@"info"][@"maxtime"] integerValue];
        [self.tableView.mj_header endRefreshing];
        NSArray *posts = [PGPostItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        for (PGPostItem *item in posts) {
            PGPostViewModel *postViewModel = [[PGPostViewModel alloc] init];
            
            postViewModel.item = item;
            [weakSelf.postViewModels addObject:postViewModel];
            
        }

        [weakSelf.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.postViewModels.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{

    PGPostViewCell *cell = [tableView dequeueReusableCellWithIdentifier:allID forIndexPath:indexPath];
    PGPostViewModel *viewModel = self.postViewModels[indexPath.row];
    cell.viewModel = viewModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    PGPostViewModel *item = self.postViewModels[indexPath.row];
    return item.cellH;
}

@end
